package com.org.mshopping.controller;

import java.util.ArrayList;
import java.util.List;

import com.org.mshopping.R;
import com.org.mshopping.dao.DatabaseService;
import com.org.mshopping.model.Category;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.GridView;

public class CategoryActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_category);
		
		GridView gCategories = (GridView) findViewById(R.id.gListCategory);
		
		DatabaseService dao = new DatabaseService(this);
        dao.open();
        
        Cursor cursor = dao.loadItems();
        if(cursor.getCount() == 0){
        	//Init database
			List<Category> categories = new ArrayList<Category>();
			categories.add(new Category(R.drawable.baby, "Baby Shop", "Shop for children"));
			categories.add(new Category(R.drawable.book, "Book Shop", "This is a book store"));
			categories.add(new Category(R.drawable.computer, "Computer Shop", "This is a computer store"));
			dao.insertItems(categories);
			cursor = dao.loadItems();
        }
        
		CategoryAdapter categoryAdapter = new CategoryAdapter(this, cursor);
		gCategories.setAdapter(categoryAdapter);
		
		dao.close();
	}
}
